package com.mzy.basics.reflectionDemo.newReflection;

import com.mzy.datepackage.M2022.May.twThree.main;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/6/20 15:44
 * @Version 1.0
 */
public class GetFieldType {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<Cat> clazz = Cat.class;

        Field name = clazz.getDeclaredField("type");
        name.setAccessible(true);
        /**
         * getGenericType() 获得属性的类型，如果类型为泛型。则直接返回。
         * 否则调用getType()
         */
        Type genericType = name.getGenericType();
        if (genericType instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) genericType;
            // 得到泛型里的class类型对象
            Object actualTypeArgument = pt.getActualTypeArguments()[0];
            Object actualTypeArgument2 = pt.getActualTypeArguments()[1];
            System.out.println("ParameterizedTpye "+actualTypeArgument);
            System.out.println("ParameterizedTpye2 "+actualTypeArgument2);
        }
        /**
         * getType()返回属性的声明类对象
         */
        Object type = name.getType();

        System.out.println(genericType);
        System.out.println(type);
    }
}
